package com.llf.Blog.ServiceImpl;

import com.llf.Blog.Pojo.Comment;
import com.llf.Blog.Mapper.CommentMapper;
import com.llf.Blog.Service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author llf
 * @since 2021-08-07
 */
@Transactional(rollbackFor=Exception.class)
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private  CommentMapper commentMapper;

    @Autowired
    private  CommentServiceImpl commentService;

    @Override
  public   List<Comment> getCommentByBlogID( Long id){

        List<Comment> comment = commentMapper.getCommentByBlogID(id);
        return  eachComment(comment);
    }

    @Override
    public Comment getCommentByParentCommentId(Long parentCommentId) {
        return commentMapper.getCommentByParentCommentId(parentCommentId);
    }

    @Override
    public int insertComment(Comment comment) {
        CommentMapper mapper = commentService.getBaseMapper();
        Long parentCommentId=comment.getParentCommentId();
        if (parentCommentId !=-1){
            comment.setParentComment(commentService.getCommentByParentCommentId(parentCommentId));
        }else {
            comment.setParentCommentId(null);
            comment.setParentComment(null);
        }
        return mapper.insert(comment);
    }

    @Override
    public int deleteComment(Long id) {
        return  commentMapper.deleteById(id);
    }

    // 循环每个评论 并将有子评论的所有评论放在一个集合 是他们只有一个共同的根

    /**
     * 循环每个顶级的评论节点
     *
     * @param comments
     * @return
     */
    private List<Comment> eachComment(List<Comment> comments) {
        List<Comment> commentsView = new ArrayList<>();
        for (Comment comment : comments) {
            Comment c = new Comment();
            BeanUtils.copyProperties(comment, c);
            commentsView.add(c);
        }
        //合并评论的各层子代到第一级子代集合中
        combineChildren(commentsView);
        return commentsView;
    }

    /**
     * @param comments root根节点，blog不为空的对象集合
     * @return
     */
    private void combineChildren(List<Comment> comments) {

        for (Comment comment : comments) {
            List<Comment> replys1 = comment.getReplyComments();
            for (Comment reply1 : replys1) {
                //循环迭代，找出子代，存放在tempReplys中
                recursively(reply1);
            }
            //修改顶级节点的reply集合为迭代处理后的集合
            comment.setReplyComments(tempReplys);
            //清除临时存放区
            tempReplys = new ArrayList<>();
        }
    }

    //存放迭代找出的所有子代的集合
    private List<Comment> tempReplys = new ArrayList<>();

    /**
     * 递归迭代，剥洋葱
     *
     * @param comment 被迭代的对象
     * @return
     */
    private void recursively(Comment comment) {

        tempReplys.add(comment);//顶节点添加到临时存放集合
        if (comment.getReplyComments().size() > 0) {
            List<Comment> replys = comment.getReplyComments();
            for (Comment reply : replys) {
                tempReplys.add(reply);
                if (reply.getReplyComments().size() > 0) {
                    recursively(reply);
                }
            }
        }
    }



}
